package cn.zhku.edu.productservice.service.impl;

import cn.zhku.edu.productservice.mapper.BrandMapper;
import cn.zhku.edu.productservice.mapper.CategoryMapper;
import cn.zhku.edu.productservice.mapper.ProductMapper;
import cn.zhku.edu.feign.pojo.bo.ParentProduct;
import cn.zhku.edu.feign.pojo.entry.Category;
import cn.zhku.edu.feign.pojo.entry.Product;
import cn.zhku.edu.productservice.service.SearchServies;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author clk
 * @Date:2021/9/13  15:18
 **/
@Service
public class SearchServiesImpl implements SearchServies {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    ProductMapper productMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CategoryMapper categoryMapper;


    @Override
    public List<ParentProduct> selectAllShop(Integer tid) {
        return productMapper.selectAllShop(tid);
    }

    @Override
    public Category selectLikeConcatByTypeName(String typeName) {
        return categoryMapper.selectLikeConcatByTypeName(typeName);
    }

    @Override
    public List<Product> selectAllBygname(String name) {
        return productMapper.selectAllBygname(name);
    }

    @Override
    public List<String> getSuggestion(String key) {
        try {
            // 1.准备请求
            SearchRequest request = new SearchRequest("online-shop-cloud");
            // 2.请求参数
            request.source().suggest(new SuggestBuilder()
                    .addSuggestion(
                            "productSuggest",
                            SuggestBuilders
                                    .completionSuggestion("suggestion")
                                    .size(10)
                                    .skipDuplicates(true)
                                    .prefix(key)
                    ));
            // 3.发出请求
            SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
            // 4.解析
            Suggest suggest = response.getSuggest();
            // 4.1.根据名称获取结果
            CompletionSuggestion suggestion = suggest.getSuggestion("productSuggest");
            // 4.2.获取options
            List<String> list = new ArrayList<>();
            for (CompletionSuggestion.Entry.Option option : suggestion.getOptions()) {
                // 4.3.获取补全的结果
                String str = option.getText().toString();
                // 4.4.放入集合
                list.add(str);
            }
            return list;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
